<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.order_number" :placeholder="$t('order.orderNumber')" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" :placeholder="$t('common.status')" class="filter-item" @change="handleDataTypeFilter">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{$t('common.search')}}
      </el-button>

    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="" :label="$t('order.orderNumber')" width="180">
        <template slot-scope="{row}">
          <span>{{ row.order_number }}</span>
        </template>
      </el-table-column>
      <el-table-column align="" :label="$t('common.website')">
        <template slot-scope="{row}">
          <div>ID：{{row.site_id}}</div>
          <div>
            <el-image
            style="width: 15px; height: 15px"
            :src="row.site.avatar"
            fit="cover"></el-image>&nbsp;
            {{ row.site.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('order.totalPaidAmount')" width="120">
        <template slot-scope="{row}">
          <span>￥{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('common.paymentMethod')" width="120">
        <template slot-scope="{row}">
          <span v-if="row.payment">{{ row.payment.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('common.number')" width="100">
        <template slot-scope="{row}">
          <span>{{ row.number }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('common.packageInformation')" width="120">
        <template slot-scope="{row}">
          <span>{{ row.package.title }}</span>
<!--          <el-tag type="success" v-if="row.valid_time">过期时间：{{row.valid_time}}</el-tag>-->
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" :label="$t('common.status')" width="110">
        <template slot-scope="{row}">
          <span>{{ row.status_text }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('order.orderTime')" width="160">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('common.operate')" width="160">
        <template slot-scope="{row}">
          <router-link :to="'/order/edit/'+row.id">
            <el-button size="small" icon="el-icon-document">
              {{$t('common.details')}}
            </el-button>
          </router-link>
          <router-link v-if="row.status==10" :to="'/order/create?order_id='+row.id+'&package_id='+row.package.package_id">
            <el-button type="primary" plain size="small" icon="el-icon-s-promotion">
              {{$t('common.goPay')}}
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
  </div>
</template>

<script>
import { getOrderList } from '@/api/order'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'OrderList',
  components: { Pagination },
  filters: {},
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 10,
        status: undefined,
        order_number: undefined
      },
      statusOptions: [
        { label: this.$t('order.statusOptions.all'), value: '' },
        { label: this.$t('order.statusOptions.obligation'), value: '10' },
        { label: this.$t('order.statusOptions.transactionClosure'), value: '11' },
        { label: this.$t('order.statusOptions.paid'), value: '20' },
        { label: this.$t('order.statusOptions.completed'), value: '30' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getOrderList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDataTypeFilter(val) {
      this.listQuery.data_type = val
      this.handleFilter()
    }
  }
}
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
